from backend.models.base import BaseModel
from backend.database import db

class Task(BaseModel):
    __tablename__ = 'tasks'
    
    name = db.Column(db.String(200), nullable=False)
    description = db.Column(db.Text)
    script_id = db.Column(db.Integer, db.ForeignKey('scripts.id'), nullable=False)
    status = db.Column(db.String(50), default='等待中')  # 等待中, 运行中, 已完成, 失败
    priority = db.Column(db.Integer, default=1)
    start_time = db.Column(db.DateTime)
    end_time = db.Column(db.DateTime)
    created_by = db.Column(db.String(100))
    
    # 关系
    script = db.relationship('Script', backref='tasks', lazy=True)
    
    def duration(self):
        if self.start_time and self.end_time:
            return (self.end_time - self.start_time).total_seconds()
        return 0